package com.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.common.BaseResponse;
import com.common.ResultUtils;
import com.domain.dto.UserSessionDto;
import com.domain.entity.User;
import com.service.UserSessionService;
import com.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.common.ErrorCode.NULL_ERROR;

@RestController
@RequestMapping("/session")
public class UserSessionController {

    @Autowired
    private UsersService usersService;
    @Autowired
    private UserSessionService userSessionService;

    /**
     * 查看用户所有的会话信息
     *
     * @return
     */
    @GetMapping
    public BaseResponse getUserAllSession(HttpServletRequest request) {
        User loginUser = usersService.getLoginUser(request);
        List<UserSessionDto> userSessionDto = userSessionService.getAllSession(loginUser);
        if (CollectionUtil.isEmpty(userSessionDto)){
            return ResultUtils.error(NULL_ERROR,"没有会话了！");
        }
        return ResultUtils.success(userSessionDto);
    }
}
